package com.doom.a3rdlibdemo.Observer;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class WebServer implements Observable{

    private static final String TAG = WebServer.class.getSimpleName();

    private List<Observer> list = null;
    private String message;

    public WebServer(){
        if(list == null)
            list = new ArrayList<Observer>();
    }

    @Override
    public void add(Observer observer) {
        list.add(observer);
    }

    @Override
    public void del(Observer observer) {
        list.remove(observer);
    }

    @Override
    public void notifyAllObservers() {
        for(Observer observer : list) {
            observer.update(message);
        }
    }

    public void publishMessage(String msg){
        this.message = msg;
        Log.d(TAG, "Server PUBLISH MESSAGE msg:"+msg);
        notifyAllObservers();
    }
}
